// 输入一个正数 n，输出所有和为 n 连续正数序列
// 例如 15 => [[1, 2, 3, 4, 5], [4, 5, 6], [7, 8]]

function continuePositiveSum(num) {
  const res = [];

  // 1. 连续正整数的第一项最大值为middle
  const mid = Math.ceil(num / 2);
  let total = 0;

  // 2. i 表示第1个数为i
  for (let i = 1; i < mid; i++) {
    // 3. j 表示对i进行j次+1
    for (let j = 2; ; j++) {
      total = ((i + (i + j - 1)) / 2) * j;
      if (total > num) {
        break;
      } else if (total === num) {
        res.push(getArray(i, j));
        break;
      }
    }
  }

  return res;
}
function getArray(i, j) {
  const arr = [];
  for (let index = 0; index < j; index++) {
    arr[index] = i + index;
  }
  return arr;
}

console.log("5连续正整数序列", continuePositiveSum(5));
console.log("8连续正整数序列", continuePositiveSum(8));
console.log("15连续正整数序列", continuePositiveSum(15));
console.log("100连续正整数序列", continuePositiveSum(100));
